﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.BLL.Extend;
using ZX.CRM.Library;
using ZX.DAL.DBExtend;
using System.Data;

namespace ZX.CRM.Bussiness
{
    public class BCrmDeliverMajor : BaseBussnies
    {
        /// <summary>
        /// 根据流程实例ID获取所有大课信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IList<V_CrmDeliverMajorInfo> GetAllDeliverMajorInfoByWorkFlowInsId(Guid WorkFlowInsId)
        {
            this.entity = new V_CrmDeliverMajorInfo();
            return this.GetEntitysBySqlComm<V_CrmDeliverMajorInfo>("select * from V_CrmDeliverMajorInfo where WorkFlowInsId='" + WorkFlowInsId + "'");
        }
        /// <summary>
        /// 获取所有大课信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IList<V_CrmDeliverMajorInfo> GetAllDeliverMajorInfo(Guid Id)
        {
            this.entity = new V_CrmDeliverMajorInfo();
            return this.GetEntitysBySqlComm<V_CrmDeliverMajorInfo>("select * from V_CrmDeliverMajorInfo where ID='" + Id + "'");
        }
        /// <summary>
        /// 获取所有英才交付单信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IList<V_CrmDeliverHrInfo> GetAllDeliverHR(Guid Id)
        {
            this.entity = new V_CrmDeliverHrInfo();
            return this.GetEntitysBySqlComm<V_CrmDeliverHrInfo>("select * from V_CrmDeliverHrInfo where WorkFlowInsId='" + Id + "'");
        }
        /// <summary>
        /// 获取所有价值业绩信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IList<V_CrmValue> GetAllCrmValue(Guid Id)
        {
            this.entity = new V_CrmValue();
            return this.GetEntitysBySqlComm<V_CrmValue>("select * from V_CrmValue where WorkFlowInsId='" + Id + "'");
        }
        /// <summary>
        /// 获取客户经理和客户总监信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IList<V_CrmValuePay> GetAllCrmValuePay(Guid cid,string caption)
        {
            this.entity = new V_CrmValuePay();
            return this.GetEntitysBySqlComm<V_CrmValuePay>("select * from V_CrmValuePay where CustomerId='" + cid + "' and Caption='" + caption + "'");
        }
        /// <summary>
        /// 获取所有项目信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IList<V_CrmDeliverMajorProInfo> GetAllDeliverMajorProInfo(Guid Id)
        {
            this.entity = new V_CrmDeliverMajorProInfo();
            return this.GetEntitysBySqlComm<V_CrmDeliverMajorProInfo>("select * from V_CrmDeliverMajorProInfo where WorkFlowInsId='" + Id + "'");
        }
        /// <summary>
        /// 获取所有内训信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IList<V_CrmDeliverTrain> GetAllDeliverTrainInfo(Guid Id)
        {
            this.entity = new V_CrmDeliverTrain();
            return this.GetEntitysBySqlComm<V_CrmDeliverTrain>("select * from V_CrmDeliverTrain where WorkFlowInsId='" + Id + "'");
        }
        /// <summary>
        /// 获取任务节点
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        public DataTable GetTaskById(string tid) {
            DbSql ds = new DbSql();
            if (ds!=null)
            {
                return ds.ExecuteQuery("select Description from dbo.HF_WorkTask where WorkTaskId='" + tid + "'"); 
            }
            return null;
        }
        /// <summary>
        /// 获取已交付数量
        /// </summary>
        /// <param name="OrderProductId"></param>
        /// <returns></returns>
        public int GetDeliverMajorPayAccount(Guid OrderProductId)
        {
            int iCount = 0;
            DbSql ds = new DbSql();
            /*iCount = Convert.ToInt32(ds.GetSingle("select COUNT(*) from CrmDeliverMajor t1,CrmMajorStudent t2 where t1.ID=t2.MajorID " +
                                 "and t2.IsSignIn='1' and t2.Exp5=0 and t1.OrderProductID='" + OrderProductId + "'").ToString());*/

            string strSql = string.Format("select dbo.fn_DeliverMajorPayAccount('{0}')", OrderProductId);
            iCount = ds.ExecuteScalar(strSql);
            return iCount;
        }
        /// <summary>
        /// 获取所有学员信息
        /// </summary>
        /// <param name="studentId">学员ID</param>
        /// <returns></returns>
        public IList<V_CrmDeliverMajorInfo> GetAllStudentByStudentId(Guid studentId)
        {
            this.entity = new V_CrmDeliverMajorInfo();
            return this.GetEntitysBySqlComm<V_CrmDeliverMajorInfo>("select * from V_CrmDeliverMajorInfo where MajorStudentId='" + studentId + "'");
        }
        /// <summary>
        /// 获取课程规划期数中的业绩归属
        /// </summary>
        /// <param name="ProductLayoutID"></param>
        /// <returns></returns>
        public IList<V_CrmDeliverMajorStudent> GetAllDeptNameByProductLayoutID(Guid ProductLayoutID)
        {
            this.entity = new V_CrmDeliverMajorStudent();
            return this.GetEntitysBySqlComm<V_CrmDeliverMajorStudent>("select distinct OrderofDeptName from V_CrmDeliverMajorStudent where ProductLayoutID='" + ProductLayoutID + "' order by OrderofDeptName asc");
        }
        /// <summary>
        /// 获取课程规划期数中的客户经理
        /// </summary>
        /// <param name="ProductLayoutID"></param>
        /// <returns></returns>
        public IList<V_CrmDeliverMajorStudent> GetAllCustomerMgrNameByProductLayoutID(Guid ProductLayoutID)
        {
            this.entity = new V_CrmDeliverMajorStudent();
            return this.GetEntitysBySqlComm<V_CrmDeliverMajorStudent>("select distinct CustomerMgrName from V_CrmDeliverMajorStudent where ProductLayoutID='" + ProductLayoutID + "' order by CustomerMgrName asc");
        }
        /// <summary>
        /// 获取课程规划期数中的团号
        /// </summary>
        /// <param name="ProductLayoutID"></param>
        /// <returns></returns>
        public IList<V_CrmDeliverMajorStudent> GetAllGroupNameByProductLayoutID(Guid ProductLayoutID)
        {
            this.entity = new V_CrmDeliverMajorStudent();
            return this.GetEntitysBySqlComm<V_CrmDeliverMajorStudent>("select distinct GroupName from V_CrmDeliverMajorStudent where ProductLayoutID='" + ProductLayoutID + "' order by GroupName asc");
        }
        /// <summary>
        /// 签到查询
        /// </summary>
        /// <param name="strId"></param>
        /// <param name="CustomerName"></param>
        /// <param name="ClassName"></param>
        /// <param name="GroupName"></param>
        /// <param name="DeptName"></param>
        /// <param name="CustomerMgrName"></param>
        /// <param name="IsDecision"></param>
        /// <param name="IsTrain"></param>
        /// <param name="IsSignIn"></param>
        /// <returns></returns>
        public IList<V_CrmDeliverMajorStudent> CheckMajorStudentDataList(string strId, string CustomerName, string ClassName,
                    string GroupName, string DeptName, string CustomerMgrName, string IsDecision, string IsTrain, string IsSignIn)
        {
            this.entity = new V_CrmDeliverMajorStudent();
            string strSql = "select * from V_CrmDeliverMajorStudent where ProductLayoutID='" + strId + "' and 1=1";
            if (CustomerName != "")
            {
                strSql += " and CustomerName like '%" + CustomerName + "%'";
            }
            if (ClassName != "")
            {
                strSql += " and ClassName like '%" + ClassName + "%'";
            }
            if (GroupName != "")
            {
                strSql += " and GroupName like '%" + GroupName + "%'";
            }
            if (DeptName != "")
            {
                strSql += " and OrderofDeptName like '%" + DeptName + "%'";
            }
            if (CustomerMgrName != "")
            {
                strSql += " and CustomerMgrName like '%" + CustomerMgrName + "%'";
            }
            if (IsDecision != "")
            {
                strSql += " and IsDecision like '%" + IsDecision + "%'";
            }
            if (IsTrain != "")
            {
                strSql += " and IsTrain like '%" + IsTrain + "%'";
            }
            if (IsSignIn != "")
            {
                strSql += " and IsSignIn like '%" + IsSignIn + "%'";
            }
            strSql += " and Exp5='0' order by GroupName,GroupNumber";
            return this.GetEntitysBySqlComm<V_CrmDeliverMajorStudent>(strSql);
        }
    }
}
